package com.liveneo.adapter.outinterface.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import com.liveneo.adapter.base.support.AdapterConstants;
import com.liveneo.adapter.base.support.LogUtils;
import com.liveneo.adapter.base.support.ThreadStore;

/**
  * @Description: 接口时间统计
  * @author liveneo-shuqw
  * @date 2016年10月29日 下午5:27:57
  * Copyright: Copyright (c) 2016 
  * Company: 北京朗泰
  * @version 3.0
  */
@Component 
@Aspect
public class ExpendAdviceAop {

	@Pointcut("execution(* com.liveneo.adapter.outinterface.web.platform..*(..))")
	private void aspectjMethod() {
	}
	
	@Pointcut("@annotation(com.liveneo.adapter.outinterface.annotation.TimeLogAop)")
	private void timeLogAop(){
	}
	
	@Around("aspectjMethod()")
	public Object aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable {
	    try {
			ThreadStore.setCurRequestType(AdapterConstants.OUT_TYPE_INTERFACE);
	        String className = joinPoint.getThis().toString();
	        String methodName = joinPoint.getSignature().getName();
	        long st = System.currentTimeMillis();
	        Object result = joinPoint.proceed();
	        LogUtils.info("[***类名:" + className + " ,方法名:" + methodName + " ,共计消耗:" + (System.currentTimeMillis() - st) + " ms ***]");
	        return result;
	    } catch (Throwable throwable) {
	    	 LogUtils.error("记录日志耗时异常!",throwable);
	    	 //LogUtils.error(throwable.getMessage(), throwable.getStackTrace());
	        throw throwable;
	    }
	}
}
